<html>
<head>
<script src="../tma.js"></script>
<script id="shader-vs" type="x-shader/x-vertex">
    attribute vec3 aVertexShape0;
    attribute vec3 aVertexShape1;
    attribute vec3 aVertexShape2;
    attribute vec3 aVertexShape3;
    attribute vec3 aVertexShape4;
    attribute vec3 aVertexShape5;
    attribute vec3 aVertexShape6;
    attribute vec3 aVertexShape7;
    attribute vec2 aTextureCoord;
    uniform float uWeightShape0;
    uniform float uWeightShape1;
    uniform float uWeightShape2;
    uniform float uWeightShape3;
    uniform float uWeightShape4;
    uniform float uWeightShape5;
    uniform float uWeightShape6;
    uniform float uWeightShape7;
    uniform mat4 uMVMatrix;
    uniform mat4 uPMatrix;
    varying vec2 vTextureCoord;
    void main() {
        vec3 vertex = (
                aVertexShape0 * uWeightShape0 +
                aVertexShape1 * uWeightShape1 +
                aVertexShape2 * uWeightShape2 +
                aVertexShape3 * uWeightShape3 +
                aVertexShape4 * uWeightShape4 +
                aVertexShape5 * uWeightShape5 +
                aVertexShape6 * uWeightShape6 +
                aVertexShape7 * uWeightShape7);
        gl_Position = uPMatrix * uMVMatrix * vec4(vertex, 1.0);
        vTextureCoord = aTextureCoord;
    }
</script>
<script id="shader-fs" type="x-shader/x-fragment">
    precision mediump float;
    varying vec2 vTextureCoord;
    uniform sampler2D uSampler;
    void main() {
        vec4 textureColor = texture2D(uSampler,
                vec2(vTextureCoord.s, vTextureCoord.t));
        gl_FragColor =
                vec4(textureColor.rgb, textureColor.a);
    }
</script>
<script>
tma.extlibs = [ 'ext/gl-matrix.js' ];
tma.onload = function () {
    var screen = new TmaScreen(TmaScreen.FULL_WIDTH, TmaScreen.FULL_HEIGHT,
            TmaScreen.MODE_3D);
    screen.attachTo(TmaScreen.BODY);

    // Initialize program with shaders.
    var program = screen.createProgram('shader-vs', 'shader-fs');

    // Initialize matrices.
    var pMatrix = mat4.create();
    var mvMatrix = mat4.create();
    mat4.perspective(45, screen.width / screen.height, 0.1, 500.0, pMatrix);
    mat4.identity(mvMatrix);
    program.setUniformMatrix('uPMatrix', pMatrix);

    var r = 0;
    var mvMatrixBase = mat4.create(mvMatrix);

    // Model data.
    var ico;
    var frame = 0;

    // Load model data.
    var xhr = new XMLHttpRequest();
//    xhr.open('GET', 'data/CDIMAGE04060.BIN');
    xhr.open('GET', 'data/CDIMAGE05513.BIN');
//    xhr.open('GET', 'data/CDIMAGE05518.BIN');
//    xhr.open('GET', 'data/CDIMAGE05519.BIN');
//    xhr.open('GET', 'data/CDIMAGE05520.BIN');
    xhr.responseType = 'arraybuffer';
    xhr.onload = function () {
        ico = new TmaModelPs2Ico();
        ico.load(this.response);
        ico.items = ico.getVertices(0).length / 3;
        for (var i = 0; i < ico.shapes; ++i) {
            var shapeArray = ico.getVertices(i);
            var vertices = screen.createBuffer(shapeArray);
            program.setAttributeArray('aVertexShape' + i, vertices, 0, 3, 0);
            program.setUniformVector('uWeightShape' + i, [ (i == 0) ? 1 : 0 ]);
        }
        for (; i < 8; ++i)
            program.setUniformVector('uWeightShape' + i, [ 0 ]);
        var coords = screen.createBuffer(ico.getCoords());
        program.setAttributeArray('aTextureCoord', coords, 0, 2, 0);
        var image = screen.createImage(128, 128, ico.getTexture());
        var texture = screen.createTexture(image, true,
                Tma3DScreen.FILTER_LINEAR);
        program.setTexture('uSampler', texture);
        setInterval(main, 15);
    };
    xhr.send();

    // Periodical rendering.
    function main () {
        // Draw
        mat4.set(mvMatrixBase, mvMatrix);
        mat4.translate(mvMatrix, [0.0, -10.0, -30.0]);
        program.setUniformMatrix('uMVMatrix', mvMatrix);
        var weight = ico.getWeights(frame);
        for (var i = 0; i < weight.length; ++i)
            program.setUniformVector('uWeightShape' + i, [ weight[i] ]);
        for (; i < 8; ++i)
            program.setUniformVector('uWeightShape' + i, [ 0 ]);
        program.drawArrays(Tma3DScreen.MODE_TRIANGLES, 0, ico.items);

        mat4.translate(mvMatrix, [-15.0, 0.0, -10.0]);
        program.setUniformMatrix('uMVMatrix', mvMatrix);
        program.drawArrays(Tma3DScreen.MODE_TRIANGLES, 0, ico.items);

        mat4.translate(mvMatrix, [30.0, 0.0, 0.0]);
        program.setUniformMatrix('uMVMatrix', mvMatrix);
        program.drawArrays(Tma3DScreen.MODE_TRIANGLES, 0, ico.items);

        // Apply Animation.
        frame = (frame + 1) % ico.frames;
    }
};
</script>
</head>
<body>
</body>
</html>
